<?php


namespace App\backend\services;


use App\backend\models\AdminOperationAllLogModel;
use App\Exceptions\InvalidRequestException;

class AdminOperationAllLogServices
{

    /**
     * 获取 - 操作日志列表
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function getList($data)
    {
        $page = !isset($data['page']) ? 1 : $data['page'];unset($data['page']);
        $limit = !isset($data['limit']) ? 10 : $data['limit'];unset($data['limit']);

        $where_data = [];
        // 账号类型
        if (isset($data['type']) && !empty($data['type'])) {
            $where_data[] = ['admin_operation_all_log.type', '=', $data['type']];
        }
        if (isset($data['name']) && !empty($data['name'])) {
            $where_data[] = ['admin_users.name', 'like', "%{$data['name']}%"];
        }
        $where_between = [];
        if (isset($data['start_time']) && !empty($data['start_time']) && isset($data['end_time']) && !empty($data['end_time']) ) {
            $start_time = strtotime($data['start_time']);
            $end_time = strtotime($data['end_time']);
            $where_between = [$start_time, $end_time];
        }

        $select_data = [
            'admin_operation_all_log.admin_id', 'admin_operation_all_log.item_name', 'admin_operation_all_log.item_content', 'admin_operation_all_log.created_at',
            'admin_users.name',
        ];
        $list = AdminOperationAllLogModel::listed($page, $limit, $where_data, $where_between, $select_data);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }

        return $list;
    }

}
